<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

</body>
<script>
    /*******************************************************************************************/
    /*****************************************斐波那契数列***************************************/
    /*******************************************************************************************/
    let fibnacci = n =>
        n <= 0 ? 0 : n == 1 ? 1 : fibnacci(n - 2) + fibnacci(n - 1)

    let result = fibnacci(10);
    console.log(result)

    /*******************************************************************************************/
    /*****************************************斐波那契数列进阶***********************************/
    /*******************************************************************************************/
    let fibnacci2 = n => {
        if (n == 0) {
            return 0;
        }
        let a1 = 0,
            a2 = 1;
        for (let i = 1; i < n; i++) {
            [a1, a2] = [a2, a1 + a2]
        }
        return a2;
    }

    let result2 = fibnacci2(10);
    console.log(result2)

    /*******************************************************************************************/
    /*****************************************斐波那契数列进阶2**********************************/
    /*******************************************************************************************/
    let fabnacci3 = n =>
        (Math.pow((1 + Math.sqrt(5)) / 2, n) - Math.pow((1 - Math.sqrt(5)) / 2, n)) / Math.sqrt(5)

    let result3 = fabnacci3(10);
    console.log(result3)

    /*******************************************************************************************/
    /*****************************************递归求斐波那契数列**********************************/
    /*******************************************************************************************/
    let getFabnacci = x => {
        if (x == 1 || x == 2) {
            return 1
        }
        return getFabnacci(x - 1) + getFabnacci(x - 2)
    }
    console.log(getFabnacci(10))
</script>